﻿<%@ WebHandler Language="C#" Class="events" %>

using System;
using System.Web;
using System.Web.SessionState;
using System.Collections.Generic;

using Newtonsoft.Json;
using VISS.Web.Frontend;
using NCT.Framework;

using VISS.API.Events;

public class events :  IHttpHandler, IRequiresSessionState  {
    
    public void ProcessRequest (HttpContext context) {
        string action = context.Request.Form["action"];

        switch (action) {
            case "sendNoel":
                SendNoel(context);
                break;
        }
    }
    
    private void SendNoel(HttpContext context) {
        string result = "{{\"result\":\"{0}\",\"html\":\"{1}\"}}";
        UserContext userContext = LoginFactory.GetCurrentUser();
        if (userContext == null) {
            FormValidateUtils.Return(string.Format(result, "-1", ""));
            return;
        }

        bool goOut = ConvertUtils.ToBool(context.Request.Form["goOut"]);

        NoelEventEnt item = NoelEventBC.GetByUserKey(userContext.UserKey);

        if (item == null) {
            item = new NoelEventEnt();
            item.UserKey = userContext.UserKey;
            item.GoOut = goOut;
            
            if(!NoelEventBC.Insert(item)) {
                FormValidateUtils.Return(string.Format(result, "0", ""));
                return;
            }
        }
        else {
            item.GoOut = goOut;
            
            if (!NoelEventBC.Update(item)) {
                FormValidateUtils.Return(string.Format(result, "0", ""));
                return;
            }
        }

        string html = string.Empty;

        if (goOut) {
            //html = "<span class='cancel'>Click vào <a href='javascript:void(0);' onclick='ChangeNoelEvent(false);'>đây</a> nếu bạn thay đổi ý định</span>";
        }
        else {
            /*html = "<div class='content'>";
            html += "<div style='font-weight: bold; text-align: center; margin-bottom: 3px;'>Bạn có muốn đi chơi Noel với những người này?</div>";
            html += "<input type='button' id='btnNoelEvent' class='button' onclick='ChangeNoelEvent(true);' />";
            html  += "</div>";*/
        }
        
        FormValidateUtils.Return(string.Format(result, "1", html));
    }
 
    public bool IsReusable {
        get {
            return false;
        }
    }

}